<?php

namespace App\Http\Controllers;

use App\Models\Expert;
use App\Models\Video;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class VideoController extends Controller
{
    public function index(){
        $oVideo=Video::orderby('id','desc')->paginate(5);
        return view('admin.video-list')
        ->with('oVideo',$oVideo);
    }
    public function create(){
        $oExpert=Expert::all();
        return view('admin.add-video')
        ->with('oExpert',$oExpert);
    }
    public function store(Request $request)
    {
        $video = $request->all();
        $rules=[
            'name'=>'required|min:2',
            'profile'=>'required|min:15',
        ];
        $message=[
            'name.required'=>'姓名不能为空',
            'name.min'=>'姓名太短，至少要有:min个字符',
            'profile.required'=>'描述不能为空',
            'profile.min'=>'描述太短，至少要有:min个字符',
        ];
        $validate = Validator::make($video,$rules,$message);
        if($validate->fails()){
            $msg=$validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }

        if($request->hasFile('thumb')){
            $file=$request->file('thumb');
            $ext=$file->extension();
            $filename=time().".".$ext;
            $file->move(public_path()."/upload",$filename);
            $video["thumb"]='/upload/'.$filename;
        }else{
            $video["thumb"]='/res/test_images/e3.jpg';
        }

        if($request->hasFile('website')){
            $file=$request->file('website');
            $ext=$file->extension();
            $filename=time().".".$ext;
            $file->move(public_path()."/upload",$filename);
            $video["website"]='/upload/'.$filename;
        }else{
            $video["website"]='/res/test_images/e3.jpg';
        }

        Video::create($video);
         return redirect('/adm/video');
    }
    public function edit($id){
        $video=Video::find($id);
        $expert=Expert::all();
        return view('admin.edit-video')
            ->with('oVideo',$video)
            ->with('oExpert',$expert);
    }
    public function update(Request $request,$id)
    {
        $data = $request->all();
        $rules=[
            'name'=>'required|min:2',
            'profile'=>'required|min:15',
        ];
        $message=[
            'name.required'=>'姓名不能为空',
            'name.min'=>'姓名太短，至少要有:min个字符',
            'profile.required'=>'描述不能为空',
            'profile.min'=>'描述太短，至少要有:min个字符',
        ];
        $validate = Validator::make($data,$rules,$message);
        if($validate->fails()){
            $msg=$validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        $video = Video::find($id);

        if($request->hasFile('thumb')){
            $file=$request->file('thumb');
            $ext=$file->extension();
            $filename=time().".".$ext;
            $file->move(public_path()."/upload",$filename);
            $video["thumb"]='/upload/'.$filename;
        }else{
            $video["thumb"]='/upload/1685344760.jpg';
        }

        if($request->hasFile('website')){
            $file=$request->file('website');
            $ext=$file->extension();
            $filename=time().".".$ext;
            $file->move(public_path()."/upload",$filename);
            $video["website"]='/upload/'.$filename;
        }else{
            $video["website"]='/res/test_images/e3.jpg';
        }
        $video->name = $request['name'];
        $video->expert_id = $request['expert_id'];
        $video->type = $request['type'];
        $video->profile = $request['profile'];
        $video->start_at = $request['start_at'];
        $video->end_at = $request['end_at'];
        $video->save();
        return json_encode(['success'=>true,'msg'=>'修改成功']);
    }
    public function destroy($id){
        $video=Video::find($id);
        $video->delete();
        return redirect('/adm/video');
    }
}
